var mysql = require('mysql');
var $conf = require('../conf/db');
var $util = require('../util/util');
var $sql = require('./userSqlMapping');

// 使用连接池，提升性能
var pool = mysql.createPool($util.extend({}, $conf.mysql));

// 向前台返回JSON方法的简单封装
var jsonWrite = function(res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        });
    } else {
        var reJson = {
            status: 200,
            count: ret.length,
            list: ret
        }
        res.json(reJson);
    }
};

module.exports = {
    upload: function(req, res, next) {

        var form = new multiparty.Form(); //解析一个上传文件
        form.uploadDir = 'imgs';
        form.maxFilesSize = 2 * 1024 * 1024;
        form.parse(req, function(err, fields, files) {
            console.log(files.originalFilename);
            console.log(files.path);
        });
        fs.rename(oldPath, newPath, (err) => {
            if (err) {
                alert('上传失败');
            } else {
                alert('上传成功');
            }
        })


    },
    add: function(req, res, next) {
        debugger;
        console.log(req)
        pool.getConnection(function(err, connection) {

            // 获取前台页面传过来的参数
            var param = req.query || req.params;

            // 建立连接，向表中插入值
            // 'INSERT INTO user(id, name, age) VALUES(0,?,?)',
            connection.query($sql.insert, [param.name, param.age], function(err, result) {
                if (result) {
                    result = {
                        code: 200,
                        msg: '增加成功'
                    };
                }

                // 以json形式，把操作结果返回给前台页面
                jsonWrite(res, result);

                // 释放连接 
                connection.release();
            });
        });
    },
    delete: function(req, res, next) {
        // delete by Id
        pool.getConnection(function(err, connection) {
            var id = +req.query.id;
            console.log(id)
            connection.query($sql.delete, id, function(err, result) {
                if (result.affectedRows > 0) {
                    result = {
                        code: 200,
                        msg: '删除成功'
                    };
                } else {
                    result = void 0;
                }
                jsonWrite(res, result);
                connection.release();
            });
        });
    },
    update: function(req, res, next) {

        pool.getConnection(function(err, connection) {

            var param = req.query || req.params;
            // if (param.name == null || param.age == null || param.id == null) {
            //     jsonWrite(res, undefined);
            //     return;
            // }

            connection.query($sql.update, [param.name, param.age, param.id], function(err, result) {

                // jsonWrite(res, result);

                // 使用页面进行跳转提示
                if (result.affectedRows > 0) {
                    res.json({
                        status: 200,
                        list: result
                    });
                } else {
                    res.json({
                        status: 500,
                        list: result
                    });
                }
                connection.release();
            });
        });

    },
    queryId: function(req, res, next) {

        pool.getConnection(function(err, connection) {
            var id = +req.query.id;
            console.log(id)
            connection.query($sql.queryById, id, function(err, result) {
                jsonWrite(res, result);
                connection.release();

            });
        });
    },
    queryAll: function(req, res, next) {

        pool.getConnection(function(err, connection) {
            connection.query($sql.queryAll, function(err, result) {
                jsonWrite(res, result);
                connection.release();
            });
        });
    }

};